package com.itheima.reggie.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.Service.EmployeeService;
import com.itheima.reggie.exception.R;
import com.itheima.reggie.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService service;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/login")
    public R<Employee> login(HttpServletRequest request ,@RequestBody Employee employee)
    {

        //密码加密
        String password=employee.getPassword();
       password= DigestUtils.md5DigestAsHex(password.getBytes());
       //查数据库有无名字
       QueryWrapper<Employee> employeeLambdaQueryWrapper=new QueryWrapper<>();
        employeeLambdaQueryWrapper.eq("Username",employee.getUsername());
      Employee em=  service.getOne(employeeLambdaQueryWrapper);
      if(em==null)
      {
          return R.error("用户名不存在");
      }
      if(!em.getPassword().equals(password))
      {
          return  R.error("密码不正确");
      }
      if(em.getStatus()==0)
      {
          return  R.error("用户被禁用");
      }
      request.getSession().setAttribute("employee",em.getId());
      return R.success(em);
    }

    @RequestMapping("/logout")
    public  R<String> logout(HttpServletRequest request){

        request.getSession().invalidate();
        return R.success("销毁成功");
    }

    @RequestMapping
   public  R<String> save(@RequestBody Employee employee,HttpServletRequest req)
   {
       System.out.println("新增员工");
       //初始密码为123456.MD5加密
       employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
     /*  //设置创建时间
       employee.setCreate_time(LocalDateTime.now());
       //设置更新时间
       employee.setUpdate_time(LocalDateTime.now());
       //设置谁添加的用户
       Long emId= (Long) req.getSession().getAttribute("employee");
       employee.setCreateUser(emId);
       employee.setUpdateUser(emId);*/
       service.save(employee);
       return R.success("添加成功");
   }
   @RequestMapping("/page")
    public  R<Page> page(int page,int pageSize,String name)
   {
       Page pageinfo=new Page(page,pageSize);
       //条件构造器加筛选条件
       LambdaQueryWrapper<Employee>  queryWrapper=new LambdaQueryWrapper();
       if(name!=null)
       {
           queryWrapper.like(Employee::getName,name);
       }
       //加一个排序条件
       queryWrapper.orderByDesc(Employee::getCreate_time);
       service.page(pageinfo,queryWrapper);

    return R.success(pageinfo);
   }

   //根据id修改员工信息
   @PutMapping
    public  R<String> update(HttpServletRequest request,@RequestBody Employee employee)
   {
     /*  employee.setUpdate_time(LocalDateTime.now());
       Long emid =(Long) request.getSession().getAttribute("employee");
       employee.setUpdateUser(emid);*/
        service.updateById(employee);
        return R.success("修改成功");
   }
   //回显功能
    //PathVariable:路径变量
   @GetMapping("/{id}")
    public  R<Employee> getByid(@PathVariable Long id)
   {
       String key="SUDDD"+id;

       redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Employee.class));
       Employee employee=null;
       if(id!=null)
       {
           employee =(Employee) redisTemplate.opsForValue().get(key);
           System.out.println("redis查数据");
          if(employee==null)
          {
            employee=service.getById(id);
              System.out.println("数据库查数据");
              if(employee!=null)
              {
                  redisTemplate.opsForValue().set(key,employee);
              } else
              {
                  //防止缓存穿透 在数据库没有查到的情况下 给redis加一个虚值
                  redisTemplate.opsForValue().set(key,"别查了别查了");
              }
          }
       }
       return R.success(employee);
   }
}
